package com.xinrui.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinrui.sunnyday.dao.CheckItemDao;
import com.xinrui.sunnyday.entity.QueryPageBean;
import com.xinrui.sunnyday.exception.SunnydayException;
import com.xinrui.sunnyday.pojo.CheckItem;
import com.xinrui.sunnyday.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Transactional
@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }

    /**
     * 添加检查项
     * @param checkItem
     */
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageInfo<CheckItem> findPage(QueryPageBean queryPageBean) {
        //模糊查询条件不为空时拼接通配符%
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())) {
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }
        //开启分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckItem> list = checkItemDao.findPage(queryPageBean.getQueryString());
        return new PageInfo<>(list);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteById(Integer id) throws SunnydayException {
        //1.查询id是否与检查组关联
        int count = checkItemDao.findById(id);
        if (count > 0) {
            throw new SunnydayException("该检查项被检查组关联！");
        }
        //2.调用删除方法
        checkItemDao.deleteById(id);
    }

    /**
     * 编辑：根据id查询信息
     * @param id
     * @return
     */
    @Override
    public CheckItem findCheckItemById(Integer id) {
        return checkItemDao.findCheckItemById(id);
    }

    /**
     * 编辑
     * @param checkItem
     */
    @Override
    public void update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }
}
